package utils.excel;

import jxl.Workbook;
import jxl.write.*;
import jxl.write.Boolean;
import jxl.write.Number;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

/**
 * Created by LiM on 2016/6/22.
 * http://www.cnblogs.com/mingforyou/archive/2013/08/26/3282922.html
 * sheet.mergeCells(0, 0, 4, 0);//添加合并单元格，第一个参数是起始列，第二个参数是起始行，第三个参数是终止列，第四个参数是终止行
 */
public class JxlWriteExcelUtils {
    /**
     * 创建工作簿
     * @param os
     * @return
     */
    public WritableWorkbook WriteExcel(OutputStream os){
        try {
            WritableWorkbook workbook = Workbook.createWorkbook(os);
            return workbook;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 创建工作簿
     * @param file
     * @return
     */
    public WritableWorkbook WriteExcel(File file){
        try {
            WritableWorkbook workbook = Workbook.createWorkbook(file);
            return workbook;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 创建Sheet页面
     * @param workbook
     * @param strSheetName
     * @param iSheetIndex
     * @return
     */
    public WritableSheet AddSheet(WritableWorkbook workbook, String strSheetName, int iSheetIndex){
        WritableSheet sheet = workbook.createSheet(strSheetName, iSheetIndex);
        return sheet;
    }

    /**
     * 添加浮点数据
     * @param sheet
     * @param iCol
     * @param iRow
     * @param dData
     * @param format
     */
    public void AddNumberCell(WritableSheet sheet, int iCol, int iRow, double dData, WritableCellFormat format){
        if(format == null) {
            try {
                Number number = new Number(iCol, iRow, dData);
                sheet.addCell(number);
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }else {
            try {
                Number number = new Number(iCol, iRow, dData, format);
                sheet.addCell(number);
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 添加整形数据
     * @param sheet
     * @param iCol
     * @param iRow
     * @param iData
     * @param format
     */
    public void AddNumberCell(WritableSheet sheet, int iCol, int iRow, long iData, WritableCellFormat format){
        if(format == null) {
            try {
                Number number = new Number(iCol, iRow, iData);
                sheet.addCell(number);
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }else {
            try {
                Number number = new Number(iCol, iRow, iData, format);
                sheet.addCell(number);
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 添加布尔数据
     * @param sheet
     * @param iCol
     * @param iRow
     * @param bData
     * @param format
     */
    public void AddBoolCell(WritableSheet sheet, int iCol, int iRow, boolean bData, WritableCellFormat format){
        if(format == null) {
            try {
                Boolean boolData = new Boolean(iCol, iRow, bData);
                sheet.addCell(boolData);
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }else {
            try {
                Boolean boolData = new Boolean(iCol, iRow, bData, format);
                sheet.addCell(boolData);
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 添加日期数据
     * @param sheet
     * @param iCol
     * @param iRow
     * @param date
     * @param format
     */
    public void AddDateTimeCell(WritableSheet sheet, int iCol, int iRow, Date date, WritableCellFormat format){
        if(format == null) {
            try {
                DateTime dateTime = new DateTime(iCol, iRow, date);
                sheet.addCell(dateTime);
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }else {
            try {
                DateTime dateTime = new DateTime(iCol, iRow, date, format);
                sheet.addCell(dateTime);
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 添加字符数据
     * @param sheet
     * @param iCol
     * @param iRow
     * @param strData
     * @param format
     */
    public void AddStringCell(WritableSheet sheet, int iCol, int iRow, String strData, WritableCellFormat format){
        if(format == null) {
            try {
                Label label = new Label(iCol, iRow, strData);
                sheet.addCell(label);
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }else {
            try {
                Label label = new Label(iCol, iRow, strData, format);
                sheet.addCell(label);
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 保存数据
     * @param workbook
     * @param os
     */
    public void SaveExcelData(WritableWorkbook workbook,OutputStream os){

        try {
            workbook.write();
            try {
                workbook.close();
            } catch (WriteException e) {
                e.printStackTrace();
            }
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 保存
     * @param workbook
     */
    public void SaveExcelData(WritableWorkbook workbook){

        try {
            workbook.write();
            try {
                workbook.close();
            } catch (WriteException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}